package chen.web.firewall;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class AbstractFilter<T extends AbstractTable<?, ?>> implements Filter {
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}
	
	private T table;

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest request = (HttpServletRequest)arg0;
		HttpServletResponse response = (HttpServletResponse)arg1;
		if(table!=null && !accept(request)){
			response.sendError(HttpServletResponse.SC_FORBIDDEN);
			return;
		} 
		chain.doFilter(request, response);
	}
	
	
	protected abstract boolean accept(HttpServletRequest request);


	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}


	public T getTable() {
		return table;
	}


	public void setTable(T table) {
		this.table = table;
	}


}
